package Q2;

import java.util.Scanner;

public class Main {

    public static int n;
    public static int m;
    public static int xs; //开始横坐标
    public static int ys; //开始纵坐标
    public static int xt; //结束横坐标
    public static int yt; //结束纵坐标
    public static char[][] ch;

    public static int[] dx = {0, 0, 1, -1};
    public static int[] dy = {1, -1, 0, 0};

    public static int ret = Integer.MAX_VALUE;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        n = scanner.nextInt();
        m = scanner.nextInt();
        xs = scanner.nextInt() - 1;
        ys = scanner.nextInt() - 1;
        xt = scanner.nextInt() - 1;
        yt = scanner.nextInt() - 1;
        ch = new char[n][m];

        for (int i = 0; i < n; i++) {
            String str = scanner.next();
            for (int j = 0; j < m; j++) {
                ch[i][j] = str.charAt(j);
            }
        }

        boolean[][] flag = new boolean[n][m];
        flag[xs][ys] = true;

        dfs(xs, ys, flag, 0);

        System.out.println(ret == Integer.MAX_VALUE ? -1 : ret);
    }

    public static void dfs(int x, int y, boolean[][] flag, int step) {

        if (x < 0 || x >= n || y < 0 || y >= m) {
            return;
        }

        if (x == xt && y == yt) {
            ret = Math.min(ret, step);
            return;
        }

        for (int i = 0; i < 4; i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];

            if (xx >= 0 && xx < n && yy >= 0 && yy < m && !flag[xx][yy] && ch[xx][yy] == '.') {
                flag[xx][yy] = true;
                dfs(xx, yy, flag, step + 1);

                //回溯
                flag[xx][yy] = false;
            }
        }
    }
}
